using System;
using System.Linq.Expressions;
using Microsoft.Xna.Framework;

namespace TPL.Xna.Common
{
    public class FloatAnimator : SpritePropertyAnimator<float>
    {
        private bool _minToMax = true;

        public float Step { get; set; }
        public float Min { get; set; }
        public float Max { get; set; }

        public FloatAnimator(Expression<Func<Sprite, float>> property)
            : base(property)
        {
        }

        public override void Initialize()
        {
            if (Min > Max)
            {
                var t = Min;
                Min = Max;
                Max = t;
            }

            //SetValue(Min);
        }

        protected override void Update(ref float value, float delta)
        {
            value += delta;
        }

        protected override float CalculateDelta()
        {
            var value = GetValue();
            if (_minToMax)
            {
                value += Step;
                if (value > Max)
                    _minToMax = false;
                return Step;
            }
            else
            {
                value -= Step;
                if (value < Min)
                    _minToMax = true;
                return -Step;
            }
        }
    }
}
